package com.zzyl.service.impl;

import com.google.common.collect.Lists;
import com.zzyl.constant.Constants;
import com.zzyl.dto.UserLoginRequestDto;
import com.zzyl.entity.Member;
import com.zzyl.mapper.MemberMapper;
import com.zzyl.properties.JwtTokenManagerProperties;
import com.zzyl.service.MemberService;
import com.zzyl.service.WechatService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service

public class MemberServiceImpI implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private WechatService wechatService;
    @Autowired
    private JwtTokenManagerProperties jwtTokenManagerProperties;
    static ArrayList DEFAULT_NICKNAME_PREFIX = Lists.newArrayList(
            "生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );
    @Override
    public LoginVo login(UserLoginRequestDto userLoginRequestDto) {
        //1、远程调用接口获取openid
        String openid = wechatService.getOpenid(userLoginRequestDto.getCode());
        //2、根据openid查询数据库member用户
        Member member = memberMapper.getByOpenId(openid);
        //3、判断member用户是否为空
        if (ObjectUtil.isEmpty(member)){
            //3.1为空，创建member对象封装openid
            //member=new Member();
           // member.setOpenId(openid);//老的方式：先创建后赋值
            //建造者模式：创建几个对象同时想赋值几个属性就赋值几个属性（创建对象同时赋值属性）好处是灵活比较快
           member = Member.builder()
                    .openId(openid)
                    // .name(XXXX)
                    .build();
        }
        //4、远程调用接口获取手机号
        String phone = wechatService.getPhone(userLoginRequestDto.getPhoneCode());
        //5、判断用户是否注册过（member的id是否为空）
        if (member.getId()!=null){
            //5.1如果注册过，判断手机号是否一致，不一致更新数据库
            if (member.getPhone().equals(phone)){
                member.setPhone(phone);
                memberMapper.update(member);
            }
        }
        //5.2没有注册，新增操作
        if (member.getId()==null){
            //5.2.1封装手机号到member
            member.setPhone(phone);
            //5.2.2随机生成一个昵称封装给member
            String name=DEFAULT_NICKNAME_PREFIX.get((int)Math.random()*DEFAULT_NICKNAME_PREFIX.size())+phone.substring(7);
            member.setName(name);
            //5.2.3插入操作
            memberMapper.save(member);
        }



        //6、封装LoginVo返回
        LoginVo loginVo=new LoginVo();
        //6.1封装ninckName
        loginVo.setNickName(member.getName());
        //6.2封装token
        //定义载荷
        Map<String,Object>map=new HashMap<>();
        map.put(Constants.JWT_USERID,member.getId());
        map.put(Constants.JWT_USERNAME,member.getName());
                //生成令牌
        String token=JwtUtil.createJWT(jwtTokenManagerProperties.getBase64EncodedSecretKey(),jwtTokenManagerProperties.getTtl(),map);
        loginVo.setToken(token);
        //7返回
        return loginVo;
    }
}
